﻿using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using DevExpressWebApplication1.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



namespace DevExpressWebApplication1.Module.CM
{
    public partial class Customer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxCallbackPanel1_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {

            string[] split = e.Parameter.Split(';');
            if (split[0] == "Edit")
            {
                txtCustomer.Text = gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "customer_name").ToString();
                txtCompany.Text = gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "company").ToString();
                txtPhone.Text = gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "phone").ToString();
                ControlUtils.SetDateEditValue(dtpDOB, gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "dob").ToString());
                txtAddress.Text = gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "address").ToString();
                ControlUtils.SetCBBValue(cmbGroup, gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "group_id").ToString());
                hdfCustomerID.Set("CusId", gvCustomer.GetRowValues(int.Parse(split[1].ToString()), "customer_id").ToString());
            }
        }

        protected void gvCustomer_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            int customerId = int.Parse(hdfCustomerID.Get("CusId").ToString());
            if (customerId > 0)
            {
                try
                {
                    CustomerUtils.Update(customerId.ToString(), txtCustomer.Text, txtCompany.Text, txtPhone.Text, ControlUtils.GetDateEditValue(dtpDOB), txtAddress.Text, ControlUtils.GetCBBValue(cmbGroup));

                    gvCustomer.DataBind();
                }
                catch (Exception ex)
                {
                    Console.Write("Update Customer fails : " + ex.Message);
                }
            }
            else
            {
                try
                {
                    CustomerUtils.Insert(txtCustomer.Text, txtCompany.Text, txtPhone.Text, ControlUtils.GetDateEditValue(dtpDOB), txtAddress.Text, ControlUtils.GetCBBValue(cmbGroup));
                    gvCustomer.DataBind();
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
        }

        protected void cpReloadGrid_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
        {

        }

        # region init
        //protected void txtDienThoai_Init(object sender, EventArgs e)
        //{
        //    ASPxSpinEdit txt = (ASPxSpinEdit)sender;
        //    GridViewDataItemTemplateContainer templateContainer = (GridViewDataItemTemplateContainer)txt.NamingContainer;
        //    txt.ID = string.Format("txtDienThoai_{0}", templateContainer.VisibleIndex);
        //    txt.ClientInstanceName = string.Format("txtDienThoai_{0}", templateContainer.VisibleIndex);
        //}

        #endregion
    }
}